//
//  QTempletFile.m
//  ImageEffect
//
//  Created by lichq on 14-2-10.
//  Copyright (c) 2014年 lichq. All rights reserved.
//

#import "QTempletFile.h"

@implementation QTempletFile

void writeTempletInfoToFile(QTempletInfo templetInfo, char *filePath){
    FILE *file;
    if((file = fopen(filePath,"w+")) == NULL){
        printf("Failed to construct file %s.!!!", filePath);
        exit(1);
    }
    
    //fwrite(&templetInfo, sizeof(QTempletInfo), 1, file);  //ERROR
    
    fwrite(&templetInfo.templetW, sizeof(int), 1, file);
    fwrite(&templetInfo.templetH, sizeof(int), 1, file);
    fwrite(&templetInfo.regionCount, sizeof(int), 1, file);
    int length = templetInfo.templetH * templetInfo.templetW * 4;
    fwrite(templetInfo.templetPixel, length * sizeof(char), 1, file);
    int length2 = templetInfo.templetH * templetInfo.templetW;
    for (int i = 0; i < templetInfo.regionCount; i++) {
        fwrite(templetInfo.regionInfo[i].regionPosition, length2 * sizeof(int), 1, file);
        fwrite(templetInfo.regionInfo[i].x, length2 * sizeof(int), 1, file);
        fwrite(templetInfo.regionInfo[i].y, length2 * sizeof(int), 1, file);
        fwrite(&templetInfo.regionInfo[i].pCount, sizeof(int), 1, file);
        fwrite(&templetInfo.regionInfo[i].xmax, sizeof(int), 1, file);
        fwrite(&templetInfo.regionInfo[i].xmin, sizeof(int), 1, file);
        fwrite(&templetInfo.regionInfo[i].ymax, sizeof(int), 1, file);
        fwrite(&templetInfo.regionInfo[i].ymin, sizeof(int), 1, file);
        fwrite(&templetInfo.regionInfo[i].existImage, sizeof(bool), 1, file);
    }
    
    fclose(file);
}

QTempletInfo getTempletInfoFromFile(char *filePath){
    FILE *file;
    if((file = fopen(filePath,"r+")) == NULL){
        printf("Failed to construct file %s.!!!", filePath);
        exit(1);
    }
    
    QTempletInfo templetInfo;
    //fread(&templetInfo, sizeof(QTempletInfo), 1, file);   //ERROR
    fread(&templetInfo.templetW, sizeof(int), 1, file);
    fread(&templetInfo.templetH, sizeof(int), 1, file);
    fread(&templetInfo.regionCount, sizeof(int), 1, file);
    
    int length = templetInfo.templetH * templetInfo.templetW * 4;
    templetInfo.templetPixel = calloc(length, 1);
    fread(templetInfo.templetPixel, length * sizeof(char), 1, file);
    
    int length2 = templetInfo.templetH * templetInfo.templetW;
    templetInfo.regionInfo = calloc(templetInfo.regionCount * sizeof(QRegionInfo), 1);
    for (int i = 0; i < templetInfo.regionCount; i++) {
        templetInfo.regionInfo[i].regionPosition = calloc(length2 * sizeof(int), 1);
        templetInfo.regionInfo[i].x = calloc(length2 * sizeof(int), 1);
        templetInfo.regionInfo[i].y = calloc(length2 * sizeof(int), 1);
        fread(templetInfo.regionInfo[i].regionPosition, length2 * sizeof(int), 1, file);
        fread(templetInfo.regionInfo[i].x, length2 * sizeof(int), 1, file);
        fread(templetInfo.regionInfo[i].y, length2 * sizeof(int), 1, file);
        fread(&templetInfo.regionInfo[i].pCount, sizeof(int), 1, file);
        fread(&templetInfo.regionInfo[i].xmax, sizeof(int), 1, file);
        fread(&templetInfo.regionInfo[i].xmin, sizeof(int), 1, file);
        fread(&templetInfo.regionInfo[i].ymax, sizeof(int), 1, file);
        fread(&templetInfo.regionInfo[i].ymin, sizeof(int), 1, file);
        fread(&templetInfo.regionInfo[i].existImage, sizeof(bool), 1, file);
    }
    
    if(fclose(file) != 0 ){
        NSLog(@"file close failed !");
    }
    
    return templetInfo;
}

@end
